/*******************************************************************************
 * Includes
 ******************************************************************************/
#include "Wed_framework.h"
#include "mem/slab/Wed_slab.h"
#include "Unity/src/unity.h"

/*******************************************************************************
 * Static
 ******************************************************************************/
static uint8_t __g_mem[256 * 1024 * 1024];
static uint8_t __g_is_mutex_ok = 0;

/*******************************************************************************
 * Code
 ******************************************************************************/

void setUp(void){

}

void tearDown(void){
}


int Wed_mutex_init(struct Wed_mutex *p_mutex){
    return __g_is_mutex_ok;
}

int Wed_mutex_lock(struct Wed_mutex *p_mutex, uint32_t time_out){
    return __g_is_mutex_ok;
}

int Wed_mutex_unlock(struct Wed_mutex *p_mutex){
    return 0;
}

int main(void){
    void *p_mem;

    UnityBegin("slab_unity_test.c");
    TEST_ASSERT_EQUAL(-WED_EINVAL, Wed_slab_init(NULL, 1024 * 1024));
    TEST_ASSERT_EQUAL(-WED_EINVAL, Wed_slab_init(__g_mem, 0));
    TEST_ASSERT_EQUAL(-WED_EINVAL, Wed_slab_init(NULL, 0));
    __g_is_mutex_ok = 1;
    TEST_ASSERT_EQUAL(1, Wed_slab_init(__g_mem, 1024 * 1024));
    __g_is_mutex_ok = 0;
    TEST_ASSERT_EQUAL(0, Wed_slab_init(__g_mem, 1024 * 1024));
    TEST_ASSERT_EQUAL(0, Wed_slab_init(__g_mem, 256 * 1024 * 1024));

    TEST_ASSERT_EQUAL(NULL, Wed_slab_mem_alloc(0));
    Wed_slab_mem_free(NULL);

    __g_is_mutex_ok = 1;
    TEST_ASSERT_EQUAL(NULL, Wed_slab_mem_alloc(60));
    __g_is_mutex_ok = 0;
    p_mem = Wed_slab_mem_alloc(60);
    TEST_ASSERT_NOT_NULL(p_mem);
    __g_is_mutex_ok = 1;
    Wed_slab_mem_free(p_mem);
    __g_is_mutex_ok = 0;
    Wed_slab_mem_free(p_mem);

    p_mem = Wed_slab_mem_alloc(130);
    TEST_ASSERT_NOT_NULL(p_mem);
    Wed_slab_mem_free(p_mem);

    p_mem = Wed_slab_mem_alloc(300);
    TEST_ASSERT_NOT_NULL(p_mem);
    Wed_slab_mem_free(p_mem);

    p_mem = Wed_slab_mem_alloc(600);
    TEST_ASSERT_NOT_NULL(p_mem);
    Wed_slab_mem_free(p_mem);

    p_mem = Wed_slab_mem_alloc(1500);
    TEST_ASSERT_NOT_NULL(p_mem);
    Wed_slab_mem_free(p_mem);

    p_mem = Wed_slab_mem_alloc(3000);
    TEST_ASSERT_NOT_NULL(p_mem);
    Wed_slab_mem_free(p_mem);

    p_mem = Wed_slab_mem_alloc(6000);
    TEST_ASSERT_NOT_NULL(p_mem);
    Wed_slab_mem_free(p_mem);

    p_mem = Wed_slab_mem_alloc(10000);
    TEST_ASSERT_NOT_NULL(p_mem);
    Wed_slab_mem_free(p_mem);


    TEST_ASSERT_EQUAL(NULL, Wed_slab_page_alloc(0));

    return (UnityEnd());
}
